//
//  GoogleOpenView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/4.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class GoogleOpenView: BaseView, GoolPassWordDelegate {
    var viewModel:GoogleOpenViewModel?
    lazy var switBtn:UISwitch = {
        let switchBtn =  UISwitch()
        switchBtn.addTarget(self, action: #selector(switBtnAction), for: UIControlEvents.touchUpInside)
        return switchBtn
    }()
    
    lazy var line:UIView = {
        let line:UIView = UIView()
        line.themeBackground = ThemeManager.ThemeColorKeys.tabLineColor
        return line
    }()
    
    override func setUpUI() {
        self.viewModel = dataSource as? GoogleOpenViewModel
        
        let nameLabel = BaseLabel(style: .oneStyle)
        nameLabel.text = kText("谷歌验证")
        nameLabel.font = UIFont.systemFont(ofSize: 15)
        self.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(20)
        }
        
   
        self.addSubview(switBtn)
        if self.viewModel?.model?.isOpenGoogle != nil {
        
            switBtn.isOn = (self.viewModel?.model?.isOpenGoogle)!
        }
        switBtn.snp.remakeConstraints { (make) in
            make.right.equalToSuperview().offset(5)
            make.centerY.equalTo(nameLabel)
            make.width.equalTo(60)
            make.height.equalTo(20)
        }
        switBtn.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
        
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(55)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(0.5)
        }
        
        
        let inforLabel = BaseLabel(style: .thirdStyle)
        inforLabel.text = kText("谷歌验证可用在登录、交易时加一层防护，能够起到保护账号安全作用")
        inforLabel.numberOfLines = 0 
        inforLabel.font = UIFont.systemFont(ofSize: 13)
        self.addSubview(inforLabel)
        inforLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(line.snp.bottom).offset(10)
        }
        
        
    }
    
    @objc func switBtnAction() {
        if self.viewModel?.model?.isGoogleBind != nil {
            if !(self.viewModel?.model?.isGoogleBind)! {
                let controller = GoogleViewController()
                controller.viewModel?.model = viewModel?.model
                viewModel?.controller?.navigationController?.pushViewController(controller, animated: true)
                switBtn.isOn = false
                return
            }
        }
        
        if self.viewModel?.model?.isOpenGoogle != nil {
            if (viewModel?.model?.isOpenGoogle)! {
                 GooglePassWordManager.creatGoolPassWord(dataSource: self, style: .googlePassWordStyle,validate: .googleStyle)
                return
            }else {
                viewModel?.closeGoogle(passWord: "", success: { (result) in
                    NotificationCenter.default.post(name: NSNotification.Name.Exchange.securityDidChange, object: nil)
                    if (self.viewModel?.model?.isOpenGoogle)! {
                        CYMBShow.showMessage(kText("谷歌验证已关闭"))
                    }else {
                        CYMBShow.showMessage(kText("谷歌验证已开启"))
                    }
                    self.viewModel?.controller?.navigationController?.popViewController(animated: true)
                }, fail: { (request) in
                    CYMBShow.showMessage(request.errorMsg)
                    if (self.viewModel?.model?.isOpenGoogle)! {
                        self.switBtn.isOn = true
                    }else {
                        self.switBtn.isOn = false
                    }
                })
                return
            }
         }
    }
    
    @objc func cancel()  {
        if AppSecurityInfo.default.model.isOpenGoogle {
            self.switBtn.isOn = true
        }else {
            self.switBtn.isOn = false
        }
    }
    
    func googleVerificationt(passWord: String) {
        if passWord.ch_length == 6 {
            viewModel?.closeGoogle(passWord: passWord, success: { (result) in
                NotificationCenter.default.post(name: NSNotification.Name.Exchange.securityDidChange, object: nil)
                if (self.viewModel?.model?.isOpenGoogle)! {
                    CYMBShow.showMessage(kText("谷歌验证已关闭"))
                }else {
                    CYMBShow.showMessage(kText("谷歌验证已开启"))
                }
                self.viewModel?.controller?.navigationController?.popViewController(animated: true)
            }, fail: { (request) in
                CYMBShow.showMessage(request.errorMsg)
                if (self.viewModel?.model?.isOpenGoogle)! {
                    self.switBtn.isOn = true
                }else {
                   self.switBtn.isOn = false
                }
            })
        }else {
            if (self.viewModel?.model?.isOpenGoogle)! {
                self.switBtn.isOn = true
            }else {
                self.switBtn.isOn = false
            }
        }
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
